package com.demoandroid.demo.file_manage;

import androidx.fragment.app.FragmentActivity;

import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;


import com.demoandroid.demo.R;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 文件管理器主文件
 * */
public class FileEx extends FragmentActivity {
    TextView pathTv;
    ListView fileList;
    File currentParent;
    File[] currentFiles;
    Button homeLv;
    File root;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_file_ex);
        // 找到id
        pathTv = findViewById(R.id.filepath);
        fileList = findViewById(R.id.file_list_item);
        homeLv = (Button) findViewById(R.id.home_back);
        // 判断是否装载了sd卡
        boolean isLoadSd = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);

        if (isLoadSd){
//            获取sd卡根目录
            root = Environment.getExternalStorageDirectory();
            currentParent = root;
//            获取当前文件夹下所有文件
            currentFiles = currentParent.listFiles();
            inflateListView(currentFiles);

        }else{
            Toast.makeText(this,"sd卡没有装载",Toast.LENGTH_SHORT).show();
        }

//        设置监听
        setListener();
    }

    /**
     * 返回对应图标
     * */
    private  Object returnIconName(File file){

        if (file.isFile()){
            /// 获取后缀
            String fileName = file.getName();
            if (fileName.endsWith(".mp4") || fileName.endsWith(".avi") || fileName.endsWith(".flv") || fileName.endsWith(".mov")){
                return R.mipmap.video;
            }else if (fileName.endsWith(".mp3") || fileName.endsWith(".m4a") || fileName.endsWith(".flac") || fileName.endsWith(".wmv")){
                return R.mipmap.music;
            }else if (fileName.endsWith(".xls") || fileName.endsWith(".xlsx")){
                return R.mipmap.excel;
            }else if (fileName.endsWith(".doc") || fileName.endsWith(".docx")){
                return R.mipmap.word;
            }else if (fileName.endsWith(".pdf")){
                return R.mipmap.pdf;
            }else{
                return  R.mipmap.file;
            }
        }else if (file.isDirectory()){
            return R.mipmap.folder;
        }else{
            return R.mipmap.wz;
        }
    }

    /**
     * 目录的操作
     * */
    private void inflateListView(File[] currentFiles) {

        List<Map<String,Object>> list  = new ArrayList<>();

        if (currentFiles==null){
            Toast.makeText(this,"获取目录、文件 失败",Toast.LENGTH_SHORT).show();
            return;
        }
//        遍历文件夹
        for (int i=0 ; i<currentFiles.length ; i++){

            Map<String,Object> map = new HashMap<>();

            map.put("fileName",currentFiles[i].getName());

            map.put("fileIcon",returnIconName(currentFiles[i]));

            list.add(map);
        }

//        创建自定义的适配器
        SimpleAdapter simpleAdapter = new SimpleAdapter(this , list,R.layout.activity_item_file,new String[]{"fileName","fileIcon"},new int[]{R.id.item_tv,R.id.item_icon});

//        设置列表集合
        fileList.setAdapter(simpleAdapter);

//        设置文本
        pathTv.setText("当前路径"+currentParent.getAbsolutePath());
    }

    /**
     * 监听事件
     * */
    private void setListener() {

        homeLv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (currentParent.getAbsolutePath().equals(root.getAbsolutePath())) {
                    FileEx.this.finish();
                }
                currentParent = currentParent.getParentFile();
                currentFiles = currentParent.listFiles();
                inflateListView(currentFiles);
            }
        });


        fileList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            position 点击时的位置
//            id 单击的行id
//            view 单击的适配器的视图
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.i("wb",String.valueOf(position));
                if (currentFiles[position].isFile()){
//                    Toast.makeText(FileEx.this,"无法打开此文件",Toast.LENGTH_SHORT).show();
                    OpenFileUtil.openFileByPath(FileEx.this,currentFiles[position].getAbsolutePath());
                }else{

//                  获取当前点击的目录下的所有文件
                    File[] temp = currentFiles[position].listFiles();
//                   修改被点击的父目录 等价于 递归
                    currentParent = currentFiles[position];
                    currentFiles = temp;
//                    数据源发生改变 重新设置适配器
                    inflateListView(currentFiles);

//                    if (temp==null || temp.length==0){
//                        Toast.makeText(FileEx.this,"无法访问",Toast.LENGTH_SHORT).show();
//                    }else{
////                   修改被点击的父目录 等价于 递归
//                        currentParent = currentFiles[position];
//                        currentFiles = temp;
////                    数据源发生改变 重新设置适配器
//                        inflateListView(currentFiles);
//                    }
                }
            }
        });
    }






}